﻿using Abp.Application.Services.Dto;
using Logistics.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.Application.Services;
using Volo.Abp.Domain.Repositories;

namespace Logistics.Car
{
    public class OwnerAppService : ApplicationService, IOwnerAppService
    {
        private readonly IRepository<Owner, int> _OwnerRepository;
        private readonly IOwnerRepository _IFieIdRepository;
        public OwnerAppService(IRepository<Owner, int> OwnerRepository, IOwnerRepository IOwnerRepository)
        {

            _OwnerRepository = OwnerRepository;
            _IFieIdRepository = IOwnerRepository;
        }
        /// <summary>
        /// 货主添加
        /// </summary>
        /// <param name="m"></param>
        /// <returns></returns>
        public async Task<OwnerDto> Create(OwnerDto m)
        {
            var Owner = new Owner
            {
                Name=m.Name,
                Phone=m.Phone,
                FieldName=m.FieldName,
                Oaddress=m.Oaddress,
                ODrivinglicense=m.ODrivinglicense,
                Oremark=m.Oremark,
                Odate=m.Odate,
            };

            await _IFieIdRepository.Create(Owner);
            return ObjectMapper.Map<Owner, OwnerDto>(Owner);
        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task DeleteAsync(int id)
        {
            await _IFieIdRepository.Delete(id);
        }

        public async Task<OwnerDto> GetCarryById(int Id)
        {
            var student = await _IFieIdRepository.GetCarryById(Id);
            return ObjectMapper.Map<Owner, OwnerDto>(student);
        }

        public async Task<PagedResultDto<OwnerDto>> GetListAsync(GetOwnerDto dto)
        {
            var vehiclelist = await _OwnerRepository.GetPagedListAsync(
               dto.SkipCount,
               dto.MaxResultCount,
               dto.Sorting);
            var totalCount = await AsyncExecuter.CountAsync(
               _OwnerRepository.WhereIf(
                   !dto.Filter.IsNullOrWhiteSpace(),
                   m => m.Name.Contains(dto.Filter)
               )
           );
            return new PagedResultDto<OwnerDto>(
             totalCount,
             ObjectMapper.Map<List<Owner>, List<OwnerDto>>(vehiclelist));
        }

        public async Task<int> Update(OwnerDto ca)
        {
            var carr = await _OwnerRepository.GetAsync(ca.Id);
            carr.Name = ca.Name;
            carr.Phone = ca.Phone;
            carr.FieldName = ca.FieldName;
            carr.Oaddress = ca.Oaddress;
            carr.ODrivinglicense = ca.ODrivinglicense;
            carr.Oremark = ca.Oremark;
            carr.Odate = ca.Odate;

            await _OwnerRepository.UpdateAsync(carr);
            return 1;
        }
    }
}
